// test_deepseek_api.js - 测试DeepSeek API连接
const dotenv = require('dotenv');
const path = require('path');
const axios = require('axios');

// 加载环境变量
dotenv.config({ path: path.resolve(__dirname, '.env') });

async function testDeepSeekAPI() {
    try {
        console.log("测试DeepSeek API连接...");
        
        const deepSeekApiKey = process.env.DEEPSEEK_API_KEY;
        const apiEndpoint = process.env.DEEPSEEK_EMBEDDING_API_ENDPOINT || "https://api.deepseek.com/embeddings";
        const modelName = process.env.DEEPSEEK_EMBEDDING_MODEL || "deepseek-embedding";
        
        console.log("API密钥设置:", deepSeekApiKey ? "已设置" : "未设置");
        console.log("API端点:", apiEndpoint);
        console.log("模型名称:", modelName);
        
        if (!deepSeekApiKey) {
            console.error("错误: DEEPSEEK_API_KEY 未设置");
            return;
        }
        
        // 测试API连接
        console.log("正在调用DeepSeek API...");
        const response = await axios.post(
            apiEndpoint,
            {
                model: modelName,
                input: "测试文本"
            },
            {
                headers: {
                    "Authorization": `Bearer ${deepSeekApiKey}`,
                    "Content-Type": "application/json"
                },
                timeout: 10000 // 10秒超时
            }
        );
        
        console.log("API调用成功!");
        console.log("响应状态:", response.status);
        console.log("响应数据结构:", Object.keys(response.data));
        
    } catch (error) {
        console.error("API调用失败:");
        console.error("错误消息:", error.message);
        console.error("错误代码:", error.code);
        if (error.response) {
            console.error("响应状态:", error.response.status);
            console.error("响应头:", error.response.headers);
            console.error("响应数据:", error.response.data);
        }
    }
}

testDeepSeekAPI();